package com.ganzi.ganziserver.service.imp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ganzi.ganziserver.core.CollectInformation;
import com.ganzi.ganziserver.core.FanState;
import com.ganzi.ganziserver.dao.*;
import com.ganzi.ganziserver.service.ReceviceService;
import com.ganzi.ganziserver.utils.GetTime;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class ReceviceServiceImp implements ReceviceService {

    private static Logger log = LoggerFactory.getLogger(ReceviceServiceImp.class);

    @Autowired
    CollectInformationDao collectDao;
    @Autowired
    SchoolInformationDao schoolDao;
    @Autowired
    FanInfromationDao fanDao;
    @Autowired
    FanCollectDao fanCollectDao;
    @Autowired
    FanStateDao stateDao;

    @Async("taskExecutor")
    @Override
    public void saveReceviceMessage(JSONObject json) {
        try{
            String channelName = json.getString("channelName");
            String ip = json.getString("ip");
            int type = json.getIntValue("type");
            if(StringUtils.isNotBlank(channelName) &&StringUtils.isNotBlank(ip)){
                JSONArray jsonArray = json.getJSONArray("state");
                if(jsonArray == null)return;
                Long schoolId = schoolDao.findSchoolIdByQueue(channelName);
                if(schoolId == null){
                    log.info(channelName+"队列，对应的学校不存在");
                    return;
                }
                for (int i = 0; i < jsonArray.size(); i++){
                    JSONObject state = (JSONObject) jsonArray.get(i);
                    String fanNumber = state.getString("fanNumber");
                    if(StringUtils.isNotBlank(fanNumber) ){
                        //根据学校id和采集器编号查询出采集器id
                        Long collectId = fanCollectDao.findFanCollectIdByschoolIdAndIp(schoolId,ip);
                        if(collectId != null){
                            Long fanid = fanDao.findFanInformationBySchoolAndCollectAndNumber(schoolId, collectId, Integer.parseInt(fanNumber));
                            if(fanid != null){
                                //如果是查询消息  就保存状态数据
                                if(type == 0){
                                    CollectInformation info = CollectInformation.builder().fanId(fanid).timePoint(GetTime.getNowTime().getTime()).information(state.toString()).build();
                                    collectDao.insertCollectInformation(info);
                                }
                                //更状态数据
                                int isOnLine = state.getInteger("isOnLine");
                                long ktTime = state.getLongValue("ktTime");
                                long newTime = state.getLongValue("newTime");
                                int switchgear = state.getIntValue("switchgear");
                                int model = state.getIntValue("model");
                                int windSpeed = state.getIntValue("windSpeed");
                                int circulate = state.getIntValue("circulate");
                                int temperature = state.getIntValue("temperature");
                                FanState fanState = FanState.builder().fanId(fanid).isOnLine(isOnLine).ktRunTime(ktTime).newRunTime(newTime)
                                        .circulate(circulate)
                                        .model(model)
                                        .temperature(temperature)
                                        .windSpeed(windSpeed)
                                        .switchgear(switchgear)
                                        .updateTime(GetTime.getNowTime()).build();
                                stateDao.updateFanState(fanState);
                            }else{
                                log.info("盘管不存在 schoolId:"+schoolId+" collectId:"+collectId+" fanNumber:"+fanNumber);
                            }
                        }else{
                            log.info("采集器不存在 schoolId:"+schoolId+" ip:"+ip);
                        }
                    }else{
                        log.info("保存状态数据失败，盘管编号为null");
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.info("保存数据到数据库异常："+e.getMessage());
        }
    }
}
